package com.metadata_system.system.response;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.metadata_system.common.db.entity.ProjectLog;
import com.metadata_system.common.enums.AllConst;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Id;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ProjectLogResponse {
    @Id
    private Integer id;

    /**
     * 项目ID
     */
    private Integer projectId;
    private String projectName;
    private Integer metadataId;
    private String metadataName;


    /**
     * 项目节点名称数组
     */
    private String projectNodeNames;

    /**
     * 用户
     */
    private Integer userId;
    private String userAvatar;
    private String username;
    private Object isUserRead;
    private Object isLock;
    private Object isRollBack;


    /**
     * 处理用户
     */
    private Integer handlerUserId;
    private String handlerUserAvatar;
    private String handlerUsername;
    private Object isHandlerUserRead;

    /**
     * 类型: Import.导入数据 Delete.删除数据
     */
    private Object type;

    /**
     * 状态: 0.待处理 1.处理通过 2.处理拒绝 3.用户取消
     */
    private Object status;

    /**
     * 拒绝原因
     */
    private String refuseReason;


    /**
     * 数据key
     */
    private String dataKey;

    /**
     * 相关文件
     */
    private String file;

    /**
     * 处理记录
     */
    private Integer historyTotal;

    private Object historyList;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createdTime;

    public static ProjectLogResponse formatData(ProjectLog projectLog) {
        return ProjectLogResponse.builder()
                .id(projectLog.getId())
                .metadataId(projectLog.getMetadataId())
                .metadataName(projectLog.getMetadataName())
                .projectId(projectLog.getProjectId())
                .projectName(projectLog.getProjectName())
                .projectNodeNames(projectLog.getProjectNodeNames())
                .userId(projectLog.getUserId())
                .handlerUserId(projectLog.getHandlerUserId())
                .type(AllConst.IdName(projectLog.getType(), AllConst.ProjectLogTypeMap))
                .status(AllConst.IdName(projectLog.getStatus(), AllConst.ProjectLogStatusArr))
                .refuseReason(projectLog.getRefuseReason())
                .isUserRead(AllConst.IdName(projectLog.getIsUserRead(), AllConst.IsStatusArr))
                .isLock(AllConst.IdName(projectLog.getIsLock(), AllConst.IsStatusArr))
                .isRollBack(AllConst.IdName(projectLog.getIsRollBack(), AllConst.IsStatusArr))
                .isHandlerUserRead(AllConst.IdName(projectLog.getIsHandlerUserRead(), AllConst.IsStatusArr))
                .dataKey(projectLog.getDataKey())
                .file(projectLog.getFile())
                .historyTotal(projectLog.getHistoryTotal())
                .historyList(new ArrayList<>())
                .createdTime(projectLog.getCreatedTime())
                .build();
    }

    public static List<ProjectLogResponse> formatData(List<ProjectLog> projectLogList) {
        List<ProjectLogResponse> userResponseList = new ArrayList<>();
        for (ProjectLog projectLog : projectLogList) {
            userResponseList.add(ProjectLogResponse.formatData(projectLog));
        }
        return userResponseList;
    }
}
